home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: Q: realloc->free?
- Date: 15 Jan 1996 15:52:50 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4ddt8i$1a1@news.iag.net>
- References: <4daa2e$oh5@axe.netdoor.com>
- NNTP-Posting-Host: pm1-orl26.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4daa2e$oh5@axe.netdoor.com>, esargent@netdoor.com says...
- >
- > This is probably a dumb question, but I can't find any specific or
- >exact information about this. Given this:
- >
- >char *a, *b;
- >
- >a = malloc(10);
- >...
- >/*
- > later we need to increase the size
- >*/
- >...
- >b = realloc(a, 100);
-
- >
- > Now let's say realloc had to move the data so a != b. Does realloc
- >free the memory previously pointed to by a or should it be explicitly
- >freed if realloc returns a new location? I checked the FAQ, but there
- >was nothing specific about realloc. Thanks for any information.
-
- If realloc succeeds, it will take care of a (it may or may not free it,
- depending on whether it actually allocated an entirely new block or was
- able to add on to the existing block). Do not free it yourself!
-
- If realloc fails, then a is still a valid pointer to the original data and
- you will need to handle it.
-
-
- In a simple program, you might add something like this to your code:
-
- if( b == NULL) /* realloc failed */
- {
- /* a is still valid. */
- fprintf( stderr, "Insufficient memeory to continue.\n");
- free(a);
- return;
- }
- else
- a = b;
-
-
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-